/*
 * @Author: swc shanwenchao12138@163.com
 * @Date: 2024-07-29 15:34:55
 * @LastEditors: swc
 * @LastEditTime: 2024-12-09 16:44:19
 * @FilePath: \xf\src\components\custom\gainKey\command.ts
 * @Description:
 * @Version: 2.0
 * @Autor: swc
 */
import { ifs, ts, Tsrv } from '@/services';

export class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: any;
  // 主组refs

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  private transformList(rows: Array<any>): Array<any> {
    return rows.map((row: any) => {
      let content = null;
      if (row[Tsrv.globalVar.fieldName.msgContent]) {
        Tsrv.utils.isJSONString(row[Tsrv.globalVar.fieldName.msgContent]) &&
          (content = JSON.parse(row[Tsrv.globalVar.fieldName.msgContent]));
      }
      return {
        id: row[Tsrv.globalVar.fieldName.ID],
        type: row[Tsrv.globalVar.fieldName.msgType],
        category: content?.category,
        status: row[Tsrv.globalVar.fieldName.msgStatus],
        sender: row[Tsrv.globalVar.fieldName.msgSender],
        sendTime: row[Tsrv.globalVar.fieldName.msgSendTime],
        title: Tsrv.getI18nBySrv(content?.title),
        note: content?.content,
        content
      };
    });
  }

  // 获取消息数据（过滤后）
  public getTableData(): Promise<boolean> {
    const self = this.self;
    const mTable = self.$refs['mTable'] as any;
    return new Promise((resolve, reject) => {
      const pageSize = self.pagerConfig.pageSize;
      const pageNo = self.pagerConfig.currentPage;
      const gpd = new ts.T_getPartDataArg();
      gpd
        .setFunName(Tsrv.globalVar.tableName.SYS_MSG)
        .setLang(self.$i18n.locale || Tsrv.globalVar.i18n.enUS);
      gpd.setPage(pageNo, pageSize).setDbType(Tsrv.globalVar.dbType.MYSQL);
      // gpd.setGlobalSearch('');
      gpd.setFilter(this.self.getFilter());
      // this.sortArr.length > 0 && gpd.setSort(this.sortArr as ifs.I_sort[]);
      // gpd.setSort(this.getSortArr());
      // 正在加载
      self.loading = true;
      Tsrv.getData<any, ifs.I_getPartDataArg>(gpd)
        .then(([b, res, rows, msg]) => {
          if (!b) {
            msg && self.$msg(msg);
            resolve(false);
          }
          // 当前未在加载
          self.loading = false;

          // 清除高亮状态
          mTable.clearCurrentRow();
          // 移除所有选中状态
          mTable.selectAll(false);
          // 如果行数 > 0 则设置第一行选中
          // 如果开启分页，设置总数
          if (self.pagerConfig) {
            self.pagerConfig.total = res.data.total;
            self.pagerConfig.pageTotal = res.data.pageTotal;
          }

          // 设置表数据
          self.msgData = rows;
          self.msgList = this.transformList(self.msgData);
          // 更新排序状态
          // setTimeout(() => {
          //   this.setSortAndFilterActive();
          //   oBaseGrid.updateSaveBtnStatus();
          // }, 0);
          resolve(true);
        })
        .catch(() => {
          self.loading = false;
        });
    });
  }

  // 删除消息
  public deleteMsg(row: any, index: number) {
    const isCanDelete = row?.status === 1;
    if (!isCanDelete) {
      this.self.$Message.info(Tsrv.getI18nByValue('未读消息不可删除'));
      return;
    }
    this.self.$Modal.confirm({
      title: Tsrv.getI18nByPath('prompt.isDeleteData') as string,
      content: Tsrv.getI18nByPath('prompt.deleteData') as string,
      onOk: () => {
        Tsrv.wsCommunicate
          .deleteMsg(row.id, row.type, row.category)
          .then(res => {
            if (res) {
              // 删除成功
              this.getTableData();
            }
          });
      }
    });
  }
}
export interface I_cmd extends T_cmd {}
export const Tcmd = new T_cmd();
// export { I_cmd, T_cmd };
